/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.modules.hires.service.impl;

import me.zhengjie.modules.hires.domain.Hires;
import me.zhengjie.utils.ValidationUtil;
import me.zhengjie.utils.FileUtil;
import lombok.RequiredArgsConstructor;
import me.zhengjie.modules.hires.repository.HiresRepository;
import me.zhengjie.modules.hires.service.HiresService;
import me.zhengjie.modules.hires.service.dto.HiresDto;
import me.zhengjie.modules.hires.service.dto.HiresQueryCriteria;
import me.zhengjie.modules.hires.service.mapstruct.HiresMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import me.zhengjie.utils.PageUtil;
import me.zhengjie.utils.QueryHelp;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;

/**
*
* @description 服务实现
* @author 毛晋辉
* @date 2020-09-06
**/
@Service
@RequiredArgsConstructor
public class HiresServiceImpl implements HiresService {

    private final HiresRepository hiresRepository;
    private final HiresMapper hiresMapper;

    @Override
    public Map<String,Object> queryAll(HiresQueryCriteria criteria, Pageable pageable){

        Page<Hires> page = hiresRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(hiresMapper::toDto));
    }

    @Override
    public List<HiresDto> queryAll(HiresQueryCriteria criteria){
        return hiresMapper.toDto(hiresRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Transactional
    public HiresDto findById(Long id) {
        Hires hires = hiresRepository.findById(id).orElseGet(Hires::new);
        ValidationUtil.isNull(hires.getId(),"Hires","id",id);
        return hiresMapper.toDto(hires);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public HiresDto create(Hires resources) {
        return hiresMapper.toDto(hiresRepository.save(resources));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(Hires resources) {
        Hires hires = hiresRepository.findById(resources.getId()).orElseGet(Hires::new);
        ValidationUtil.isNull( hires.getId(),"Hires","id",resources.getId());
        hires.copy(resources);
        hiresRepository.save(hires);
    }

    @Override
    public void deleteAll(Long[] ids) {
        for (Long id : ids) {
            hiresRepository.deleteById(id);
        }
    }

    @Override
    public void download(List<HiresDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (HiresDto hires : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("发布人id", hires.getUserId());
            map.put("标题", hires.getTitle());
            map.put("职位类型", hires.getJobType());
            map.put("薪资描述", hires.getSalary());
            map.put("招聘人数", hires.getNum());
            map.put("单位地址", hires.getAddress());
            map.put("工作职责（json）", hires.getJobDuty());
            map.put(" createTime",  hires.getCreateTime());
            map.put(" updateTime",  hires.getUpdateTime());
            map.put(" createBy",  hires.getCreateBy());
            map.put(" updateBy",  hires.getUpdatedBy());
            map.put("状态：0待审核；1审核通过；2审核失败", hires.getStatus());
            map.put("生效时间", hires.getStartTime());
            map.put("失效时间", hires.getEndTime());
            map.put("任职要求（json）", hires.getJobRequires());
            map.put("福利待遇", hires.getJobBenefits());
            map.put("联系电话", hires.getTel());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}